package initialize

import "github.com/spf13/viper"

type Config struct {
	Service  *ServiceConfig `yaml:"service"`
	DbConfig *DbConfig      `yaml:"dbConfig"`
	Wechat   *WechatConfig  `yaml:"wechat"`
}

type ServiceConfig struct {
	Port string `yaml:"port"`
	Addr string `yaml:"addr"`
}

type DbConfig struct {
	Drive  string `yaml:"drive"`
	Host   string `yaml:"host"`
	Port   int    `yaml:"port"`
	User   string `yaml:"user"`
	Pass   string `yaml:"pass"`
	DbName string `yaml:"dbName"`
}

type WechatConfig struct {
	AppId     string `yaml:"appid"`
	AppSecret string `yaml:"appSecret"`
}

func InitConfig() (*Config, error) {
	viper.SetConfigFile("./config.yaml")
	err := viper.ReadInConfig()
	if err != nil {
		return nil, err
	}

	var config Config
	err = viper.Unmarshal(&config)
	if err != nil {
		return nil, err
	}
	return &config, nil
}
